package com.usian.user.service.imp;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.user.dtos.ApUserVo;
import com.usian.model.user.pojos.ApUser;
import com.usian.user.mapper.AdUserRegisterMapper;
import com.usian.user.service.AdUserRegisterService;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * 文件名:AdUserRegisterServiceimp
 * 创建者: 闫俊涛
 * 创建时间:2022/6/29 11:56
 * 描述:这是一个实例
 */
@Service
public class AdUserRegisterServiceimp  extends ServiceImpl<AdUserRegisterMapper,ApUser> implements AdUserRegisterService {


    @Autowired
    private RabbitTemplate  rabbitTemplate;
    @Autowired
    private  AdUserRegisterMapper adUserRegisterMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public ResponseResult sendCode(String phone) {
       AdUser adUser =   adUserRegisterMapper.findUserByphone(phone);
       if (adUser==null){
           rabbitTemplate.convertAndSend("java.driect.exchange","java.first.queue",phone);
           return  ResponseResult.okResult(null);
       }{
            return ResponseResult.errorResult(300,"手机号已被注册");
        }

    }

    @Override
    public AdUser findByPhone(String phone) {
        AdUser adUser = adUserRegisterMapper.findUserByphone(phone);
        return adUser;
    }

    @Override
    public ResponseResult register(ApUserVo apUserVo) {
        // 用户输入手机号 验证进行注册
        // 验证码是否超期 redis 中是否存在
        String redisCode = stringRedisTemplate.opsForValue().get(apUserVo.getPhone());
        // 输入的和redis 比较是否一致
        if (StringUtils.isNotEmpty(redisCode)){
            // 如果一致就注册成功
            if (redisCode.equals(apUserVo.getCode())){

                ApUser apUser = new ApUser();
                BeanUtils.copyProperties(apUserVo, apUser);
                apUser.setName(apUserVo.getPhone());
                int code = RandomUtil.randomInt(1000, 9999);
                apUser.setSalt(code + "");
                String pswd = apUserVo.getPhone() + code + "";
                String saltPswd = DigestUtils.md5DigestAsHex(pswd.getBytes());
                apUser.setPassword(saltPswd);
                apUser.setSex(false);
                apUser.setIdentityAuthentication(false);
                apUser.setStatus(true);
                apUser.setFlag(1);
                adUserRegisterMapper.insert(apUser);
                return ResponseResult.okResult(0, "注册成功");
            }else{
                return ResponseResult.errorResult(300,"验证码输入错误");
            }
        }else {
            return ResponseResult.errorResult(301,"验证码已过期");
        }
    }

}
